using InputDto = NtRuirun.Mes.Wms.Dtos.MaterialInputDto;
using OutputDto = NtRuirun.Mes.Wms.Dtos.MaterialOutputDto;
using Entity = NtRuirun.Mes.Wms.Entities.MaterialEntity;

namespace NtRuirun.Mes.Api.Areas.Admin.Controllers.Wms
{
    /// <summary>
    /// 管理-物料信息
    /// </summary>
    [Description("管理-物料信息")]
    [ModuleInfo(Code = "Material", Name = "物料信息", OrderNo = 99, Position = "Wms", PositionName = "仓储模块模块")]
    public class MaterialController: AdminControllerBase
    {
        /// <summary>
        /// 获取 物料信息服务
        /// </summary>
        protected MaterialStore MaterialStore => ServiceProvider.GetService<MaterialStore>();

        /// <summary>
        /// 读取物料信息列表
        /// </summary>
        [ModuleInfo]
        [HttpPost]
        [Description("读取")]
        public PageData<OutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<Entity, bool>> predicate = FilterService.GetExpression<Entity>(request.FilterGroup);
            var page = CacheService.ToPageCache<Entity, OutputDto>(MaterialStore.Entities, predicate, request.PageCondition, function);

            return page.ToPageData();
        }

        /// <summary>
        /// 新增物料信息实体
        /// </summary>
        /// <param name="dtos">新增物料信息信息</param>
        /// <returns>JSON操作结果</returns>
        [Description("新增")]
        [DependOnFunction(nameof(Read))]
        [ModuleInfo]
        [UnitOfWork]
        [HttpPost]
        public async Task<AjaxResult> Create(InputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<OutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await MaterialStore.InsertByDtoAsync(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<OutputDto>());
            }
            return AjaxResult.Success(outputDtos, string.Format("实体“{0}”创建成功", ids.ExpandAndToString()));
        }
    }
}